/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elektra;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author adee
 */
public class RawCommandExecutor {

    private static final Logger LOG = Logger.getLogger(RawCommandExecutor.class.getName());
    private String command;
    private StringBuilder output;
    private Process processSh;
    
    
    
    private void initShProcess(){
        try {
            this.processSh = Runtime.getRuntime ().exec ("/bin/sh");
            this.processSh.waitFor();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, "Nem futtathato a /bin/sh", ex);
        }    
    }
    
    public RawCommandExecutor(String command)  {
        this.command=command;
        this.output = new StringBuilder();
    }
    
 
    public void execute() throws Exception{
        
        LOG.info("Trying to execute:"+this.command);  
        
        this.processSh = Runtime.getRuntime ().exec (this.command);
        this.processSh.waitFor();
        
        try{
          BufferedReader reader =  new BufferedReader(new InputStreamReader(this.processSh.getInputStream()));
          String line;
          while ((line=reader.readLine())!=null){
            this.output.append("\n");
            this.output.append(line);
          }
          
          LOG.info("Command Executed:"+this.command);  

        }catch(Exception ex){
           LOG.warning("Failed to read output");
        }
        
        
    }
    
    public String getOutput(){
        return this.output.toString();
    }
    
}
